package com.hrm.service;

import com.hrm.dao.AdminDao;
import com.hrm.dao.EmployeeDao;
import com.hrm.dto.Envelope;
import com.hrm.entity.AdminEntity;
import com.hrm.util.PasswordUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * Create by zekdot on 20-2-24.
 */
public class AdminService {


    private AdminDao adminDao;

    private EmployeeDao employeeDao;
    public AdminService(){
        adminDao = new AdminDao();
        employeeDao = new EmployeeDao();
    }

    /**
     * 登录
     * @param id 管理员id
     * @param password 管理员密码
     * @return 登录是否成功
     */
    public Envelope login(String id,String password){
        AdminEntity adminEntity;
        try {
            adminEntity = adminDao.get(id);

        } catch (Exception e) {
            e.printStackTrace();
            return Envelope.dbError;
        }
        if(adminEntity == null || !adminEntity.getPassword().equals(PasswordUtil.getMd5(password))){
            return new Envelope(1,"用户名或密码错误",null);
        }
        adminEntity.setPassword(null);
        return new Envelope(adminEntity);
    }

    /**
     * 修改管理员密码
     * @param id 管理员id
     * @param oriPassword 原密码
     * @param newPassword 新密码
     * @return
     */
    public Envelope changePassword(String id,String oriPassword,String newPassword){
        AdminEntity adminEntity;
        try {
            adminEntity = adminDao.get(id);
        } catch (Exception e) {
            e.printStackTrace();
            return Envelope.dbError;
        }
        if(!adminEntity.getPassword().equals(PasswordUtil.getMd5(oriPassword))){
            return new Envelope(1,"原密码输入错误",null);
        }
        Map<String,Object> map = new HashMap<>();
        map.put("password",PasswordUtil.getMd5(newPassword));
        try {
            adminDao.update(id,map);
        } catch (Exception e) {
            e.printStackTrace();
            return Envelope.dbError;
        }
        return Envelope.success;
    }

    public static void main(String args[]){
        AdminService adminService = new AdminService();
//        adminService.addEmplyee("zekdot","123456","test","男")
 //       adminService.addEmplyer("test","123456","test","男",20,"13012345678");
//        System.out.println(adminService.login("zekdot","123456").toString());
    }
}
